package com.dao;

import com.model.UserBean;
import java.sql.*;
import java.util.ArrayList;

import com.util.*;

public class UserDao {

	public UserBean checkUser(String userName, String pass){

		ResultSet rs = null;
		Connection conn = DataUtil.getConnection();
		try {
			CallableStatement cstmt = conn.prepareCall("{ call sp_CheckUser(?, ?)}");
			cstmt.setString(1, userName);
			cstmt.setString(2, pass);
			
			rs = DataUtil.executeStore(cstmt);
			
			if (rs.next()) {
				UserBean objUser = new UserBean();
				objUser.setUserID(rs.getInt("User_Id"));
				objUser.setUserName(rs.getString("UserName"));
				objUser.setFullName(rs.getString("FullName"));
				objUser.setEmail(rs.getString("Email"));
				objUser.setJoinDate(rs.getString("Join_Date"));
				objUser.setRoleID(rs.getInt("Role_Id"));
				return objUser;
			}

		} catch (SQLException e) {
			
			e.printStackTrace();
		} finally {
			DataUtil.disconnected();
		}
		return null;
	}
	
	public ArrayList<UserBean> getUserName(){
		ArrayList<UserBean> userList = new ArrayList<UserBean>();
		ResultSet rs = null;
		Connection conn = DataUtil.getConnection();
		try{
			CallableStatement cstmt = conn.prepareCall("{ call }");
			rs = DataUtil.executeStore(cstmt);
			
			if(rs.next()){
				UserBean objUser = new UserBean();
				objUser.setUserID(rs.getInt("User_Id"));
				objUser.setUserName(rs.getString("UserName"));
				objUser.setFullName(rs.getString("FullName"));
				objUser.setEmail(rs.getString("Email"));
				objUser.setJoinDate(rs.getString("Join_Date"));
				objUser.setRoleID(rs.getInt("Role_Id"));
				userList.add(objUser);				
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return userList;
	}
}
